//
//  STMeViewController.swift
//  RouterPushProtocol
//
//  Created by Toj on 2/21/22.
//

import UIKit
import TTCOM_iOS
import MessageUI
import Kingfisher

// MARK: - 懒加载(属性), 变量, 初始化init
class STMeViewController: STBaseViewController {
    
    private let viewModel = MeViewModel()
    
    override var pagePath: String {
        return "startimes://me"
    }
    
    private lazy var tableView: UITableView = {
        let view = UITableView()
        view.backgroundColor = .white
        if #available(iOS 15.0, *) {
            view.sectionHeaderTopPadding = CGFloat.cg_cipher
        }
        view.rowHeight = 44
        view.dataSource = self
        view.delegate = self
        view.contentInset = UIEdgeInsets(top: .nav_barFullHeight, left: 0, bottom: CGFloat.tab_barFullHeight, right: 0)
        view.register(cellClass: VCCell.self)
        view.register(viewClass: MeHeaderView.self)
        view.register(viewClass: UITableViewHeaderFooterView.self)
        
        return view
    }()
}

// MARK: - 生命周期
extension STMeViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
}

// MARK: - 点击事件
@objc private extension STMeViewController {
    
}

// MARK: - Private
private extension STMeViewController {
    
}

// MARK: - UITableViewDataSource
extension STMeViewController: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        let sections = viewModel.classSections
        return sections.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let section = viewModel.classSections[section]
        let cellModels = section.cellModels
        return cellModels.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let section = viewModel.classSections[indexPath.section]
        let cellModels = section.cellModels
        
        let cell = tableView.dequeueReusableCell(withReuseIdentifierClass: VCCell.self)
        // print("-->toj: address:" + cell.cm_objectAddress + ", num:" + String(indexPath.row))
        cell.cellModel = cellModels[indexPath.row]
      
        return cell
    }
}

// MARK: - UITableViewDelegate
extension STMeViewController: UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let model = tableView.cellForItem(at: indexPath, convertTo: VCCell.self)?.cellModel else { return }
        
        guard let cls = UIViewController.vc_reflection(className: model.name) else { return }
        navigationController?.pushViewController(cls, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        let section = viewModel.classSections[section]
        return section.headerSize.height
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let section = viewModel.classSections[section]

        let view = tableView.dequeueReusableHeaderFooterView(withIdentifierClass: MeHeaderView.self)
        view.titleLabel.text = section.title

        return view
    }

    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = tableView.dequeueReusableHeaderFooterView(withIdentifierClass: UITableViewHeaderFooterView.self)
        return view
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        let section = viewModel.classSections[section]
        return section.footerSize.height
    }
}

// MARK: - 初始化设置
private extension STMeViewController {
    
    func setupMakeAddSubviews() {
        
        view.addSubview(tableView)
    }
    
    func setupMakeLayoutSubviews() {
        
        tableView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(view)
        }
    }
    
    func setupMakeInitialize() {
        
        tt_isNavigationBarHidden = true
        navigationBar.leftBarView?.isHidden = true
        view.backgroundColor = .white
        viewModel.setupSections {
            self.tableView.reloadData()
        }
    }
}
